#include<bits/stdc++.h>
using namespace std;

int a[210];
int num[210];
int pre[210];

int main()
{
	int n;
	cin>>n;
	for(int i = 1;i <= n;i++)
		cin>>a[i];
	num[n] = 1;
	for(int i = n-1;i >= 1;i--)
	{
		for(int j = i+1;j <= n;j++)
		{
			if(a[j] >= a[i])
			{
				if(num[i] <= num[j]+1)
				{
					num[i] = num[j]+1;
					pre[i] = j;
				}
			}
			if(j == n && !num[i])
				num[i] = 1;
		}
	}
	int index = max_element(num+1, num+n+1)-num;
	cout<<"max="<<num[index]<<endl;
	while(index)
	{
		cout<<a[index]<<" ";
		index = pre[index];
	}
    return 0;
}

